package com.gzc.just.play.last.war.centralserver.team;

/**
 * 队伍邀请 - 表示一个待处理的队伍邀请
 * 
 * @author AI编程助手
 * @version 1.0
 */
public class TeamInvitation {
    
    /**
     * 邀请状态
     */
    public enum InvitationStatus {
        PENDING,    // 待处理
        ACCEPTED,   // 已接受
        REJECTED,   // 已拒绝
        EXPIRED,    // 已过期
        CANCELED    // 已取消
    }
    
    private long invitationId;
    private long teamId;
    private long inviterId;
    private long inviteeId;
    private InvitationStatus status;
    private long creationTime;
    private long expirationTime;
    private static final long INVITATION_TIMEOUT = 300000; // 5分钟
    
    /**
     * 构造函数
     * 
     * @param invitationId 邀请ID
     * @param teamId 队伍ID
     * @param inviterId 邀请者ID
     * @param inviteeId 被邀请者ID
     */
    public TeamInvitation(long invitationId, long teamId, long inviterId, long inviteeId) {
        this.invitationId = invitationId;
        this.teamId = teamId;
        this.inviterId = inviterId;
        this.inviteeId = inviteeId;
        this.status = InvitationStatus.PENDING;
        this.creationTime = System.currentTimeMillis();
        this.expirationTime = creationTime + INVITATION_TIMEOUT;
    }
    
    /**
     * 检查邀请是否已过期
     * 
     * @return 是否过期
     */
    public boolean isExpired() {
        if (status == InvitationStatus.PENDING) {
            if (System.currentTimeMillis() > expirationTime) {
                status = InvitationStatus.EXPIRED;
                return true;
            }
        }
        return false;
    }
    
    /**
     * 接受邀请
     * 
     * @return 是否成功接受
     */
    public boolean accept() {
        if (status == InvitationStatus.PENDING && !isExpired()) {
            status = InvitationStatus.ACCEPTED;
            return true;
        }
        return false;
    }
    
    /**
     * 拒绝邀请
     * 
     * @return 是否成功拒绝
     */
    public boolean reject() {
        if (status == InvitationStatus.PENDING && !isExpired()) {
            status = InvitationStatus.REJECTED;
            return true;
        }
        return false;
    }
    
    /**
     * 取消邀请
     * 
     * @return 是否成功取消
     */
    public boolean cancel() {
        if (status == InvitationStatus.PENDING) {
            status = InvitationStatus.CANCELED;
            return true;
        }
        return false;
    }
    
    // Getters
    
    public long getInvitationId() {
        return invitationId;
    }
    
    public long getTeamId() {
        return teamId;
    }
    
    public long getInviterId() {
        return inviterId;
    }
    
    public long getInviteeId() {
        return inviteeId;
    }
    
    public InvitationStatus getStatus() {
        return status;
    }
    
    public long getCreationTime() {
        return creationTime;
    }
    
    public long getExpirationTime() {
        return expirationTime;
    }
    
    /**
     * 获取剩余有效时间（毫秒）
     * 
     * @return 剩余时间，如果已过期则返回0
     */
    public long getRemainingTime() {
        long remaining = expirationTime - System.currentTimeMillis();
        return Math.max(0, remaining);
    }
    
    @Override
    public String toString() {
        return String.format("TeamInvitation{id=%d, teamId=%d, inviter=%d, invitee=%d, status=%s}",
                           invitationId, teamId, inviterId, inviteeId, status);
    }
}
